﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading;
using System.Configuration;

namespace Infrastructure.Web.Services
{
    public class SessionService: ISessionService
    {
        public T GetValue<T>(string key, string source)
        {
            T result = default(T);
            switch (source)
            {
                case SessionSource.Session:
                    result = (T)HttpContext.Current.Session[key];
                    break;
                case SessionSource.Profile:
                    if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
                    {
                        var profile = ProfileCommon.Create(Thread.CurrentPrincipal.Identity.Name, true);
                        result = (T) profile.GetPropertyValue(key);

                    }
                    break; 
             
                default:
                    break;
            }
            return result;
        }
        public void SetValue<T>(string key, T value, string source)
        {
            switch (source)
            {
                case SessionSource.Session:
                    HttpContext.Current.Session[key] = value;
                    break;
                case SessionSource.Profile:
                    if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
                    {
                        var profile = ProfileCommon.Create(Thread.CurrentPrincipal.Identity.Name, true);
                        profile.SetPropertyValue(key, value);
                        profile.Save(); 
                    }
                    break;  
                default:
                    break;
            }
        }

        private string _Culture; 
        public string Culture
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_Culture))
                {
                    _Culture = this.GetValue<string>("Culture", SessionSource.Profile);
                    if (string.IsNullOrWhiteSpace(_Culture))
                    {
                        _Culture = Thread.CurrentThread.CurrentCulture.Name;
                    }
                }                
                return _Culture;                 
            }
            set
            {
                this.SetValue<string>("Culture", value, SessionSource.Profile);
                _Culture = value; 
            }
        }
        private string _UICulture;
        public string UICulture
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this._UICulture))
                {
                     _UICulture = this.GetValue<string>("UICulture", SessionSource.Profile);
                    if (string.IsNullOrWhiteSpace(_UICulture))
                    {
                        _UICulture = Thread.CurrentThread.CurrentUICulture.Name;
                    }
                }                
                return _UICulture; 
            }
            set
            {
                this.SetValue<string>("UICulture", value, SessionSource.Profile);
                this._UICulture = null; 
            }
        }


        public string ApplicationName
        {
            get
            {
                return ConfigurationManager.AppSettings[Constants.SessionNames.ApplicationName];
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
